package com.shangdiao.sd_server.c_client.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shangdiao.sd_server.constant.Constant;
import com.shangdiao.sd_server.constant.ErrorCode;
import com.shangdiao.sd_server.generator.pojo.SdInternship;
import com.shangdiao.sd_server.generator.pojo.SdInternshipExample;
import com.shangdiao.sd_server.generator.pojo.SdPInternship;
import com.shangdiao.sd_server.generator.pojo.SdPInternshipExample;
import com.shangdiao.sd_server.generator.pojo.SdPParttime;
import com.shangdiao.sd_server.generator.pojo.SdPParttimeExample;
import com.shangdiao.sd_server.generator.pojo.SdParttime;
import com.shangdiao.sd_server.generator.pojo.SdParttimeExample;
import com.shangdiao.sd_server.generator.pojo.SdPerson;
import com.shangdiao.sd_server.generator.pojo.SdPersonBase;
import com.shangdiao.sd_server.pojo.ResponseMsg;
import com.shangdiao.sd_server.pojo.Row;
import com.shangdiao.sd_server.service.CompanyService;
import com.shangdiao.sd_server.service.PersonService;
import com.shangdiao.sd_server.service.PostService;
import com.shangdiao.sd_server.util.DateUtil;
import com.shangdiao.sd_server.util.RequestUtil;
import com.shangdiao.sd_server.util.StringUtil;

/**
 * 企业简历管理
 * @author mary
 *
 */
@Controller
@RequestMapping("/company/oper")
public class CompanyOperController {
	
	@Autowired
	PersonService personService;
	
	@Autowired
	PostService postService;
	
	@Autowired
	CompanyService companyService;
	
	
	@RequestMapping(value = { "/selectMyResume.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectMyResume(HttpServletRequest request, HttpServletResponse response) {
		try{
			List<Row> resultList = new ArrayList<Row>();
			Integer companyId = RequestUtil.getInt(request, "uid");
			//实习
			SdInternshipExample internshipExample = new SdInternshipExample();
			internshipExample.or().andCompanyIdEqualTo(companyId)
				.andStatusEqualTo(Constant.STATUS_ON)
				.andDelEqualTo(Constant.DEL_NO);
			internshipExample.setOrderByClause("create_date desc");
			List<SdInternship> internshipList = postService.selectInternshipList(internshipExample);
			for(SdInternship internship : internshipList){
				Row result = new Row();
				result.put("postFlag", Constant.POST_FLAG_INTERNSHIP);//实习
				Integer internshipId = internship.getInternshipId();
				result.put("postId", internshipId);
				result.put("postName", internship.getInternshipName());
				result.put("postTime", DateUtil.DateToString(internship.getCreateDate(), DateUtil.LONG_DATE_FORMAT));
				//全部
				SdPInternshipExample pInternshipExample = new SdPInternshipExample();
				pInternshipExample.or().andInternshipIdEqualTo(internshipId)
					.andDelCEqualTo(Constant.DEL_NO);
				int allCount = postService.countPInternship(pInternshipExample);
				if(allCount == 0){
					continue;
				}
				result.put("allCount", allCount);
				//未查看
				pInternshipExample = new SdPInternshipExample();
				pInternshipExample.or().andInternshipIdEqualTo(internshipId)
					.andStatusEqualTo(Constant.INTERNSHIP_STATUS_APPLY);
				int uncheckCount = postService.countPInternship(pInternshipExample);
				result.put("uncheckCount", uncheckCount);
				//已查看
				pInternshipExample = new SdPInternshipExample();
				pInternshipExample.or().andInternshipIdEqualTo(internshipId)
					.andStatusEqualTo(Constant.INTERNSHIP_STATUS_CHECKED);
				int checkedCount = postService.countPInternship(pInternshipExample);
				result.put("checkedCount", checkedCount);
				//已面试
				pInternshipExample = new SdPInternshipExample();
				pInternshipExample.or().andInternshipIdEqualTo(internshipId)
					.andStatusEqualTo(Constant.INTERNSHIP_STATUS_INTERVIEWED);
				int interviewedCount = postService.countPInternship(pInternshipExample);
				result.put("interviewedCount", interviewedCount);
				resultList.add(result);
			}
			//兼职
			SdParttimeExample parttimeExample = new SdParttimeExample();
			parttimeExample.or().andCompanyIdEqualTo(companyId)
				.andStatusEqualTo(Constant.STATUS_ON)
				.andDelEqualTo(Constant.DEL_NO);
			internshipExample.setOrderByClause("create_date desc");
			List<SdParttime> parttimeList = postService.selectParttimeList(parttimeExample);
			for(SdParttime parttime : parttimeList){
				Row result = new Row();
				result.put("postFlag", Constant.POST_FLAG_PARTTIME);//实习
				Integer parttimeId = parttime.getParttimeId();
				result.put("postId", parttimeId);
				result.put("postName", parttime.getDicParttimeFieldName());
				result.put("postTime", DateUtil.DateToString(parttime.getCreateTime(), DateUtil.LONG_DATE_FORMAT));
				//全部
				SdPParttimeExample pParttimeExample = new SdPParttimeExample();
				pParttimeExample.or().andParttimeIdEqualTo(parttimeId)
					.andDelCEqualTo(Constant.DEL_NO);
				int allCount = postService.countPParttime(pParttimeExample);
				if(allCount == 0){
					continue;
				}
				result.put("allCount", allCount);
				//未查看
				pParttimeExample = new SdPParttimeExample();
				pParttimeExample.or().andParttimeIdEqualTo(parttimeId)
					.andStatusEqualTo(Constant.INTERNSHIP_STATUS_APPLY);
				int uncheckCount = postService.countPParttime(pParttimeExample);
				result.put("uncheckCount", uncheckCount);
				//已查看
				pParttimeExample = new SdPParttimeExample();
				pParttimeExample.or().andParttimeIdEqualTo(parttimeId)
					.andStatusEqualTo(Constant.INTERNSHIP_STATUS_CHECKED);
				int checkedCount = postService.countPParttime(pParttimeExample);
				result.put("checkedCount", checkedCount);
				//已上岗
				pParttimeExample = new SdPParttimeExample();
				pParttimeExample.or().andParttimeIdEqualTo(parttimeId)
					.andStatusEqualTo(Constant.INTERNSHIP_STATUS_INTERVIEWED);
				int workedCount = postService.countPParttime(pParttimeExample);
				result.put("workedCount", workedCount);
				resultList.add(result);
			}
			return new ResponseMsg("操作成功", ErrorCode.OK, resultList);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("操作失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/selectMyResumeList.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectMyResumeList(HttpServletRequest request, HttpServletResponse response) {
		try{
			List<Row> resultList = new ArrayList<Row>();
			Integer companyId = RequestUtil.getInt(request, "uid");
			Integer postId = RequestUtil.getInt(request, "postId");
			Integer postFlag = RequestUtil.getInt(request, "postFlag");//1实习2兼职
			String status = RequestUtil.get(request, "status");//状态 为空表示查看全部
			int curPage = RequestUtil.getInt(request, "curPage", Constant.DEFAULT_CUR_PAGE);
			int pageSize = RequestUtil.getInt(request, "pageSize", Constant.DEFAULT_PAGE_SIZE);
			if(companyId == null || postId == null || postFlag == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			Integer start = (curPage - 1) * pageSize;
			if(postFlag == Constant.POST_FLAG_INTERNSHIP){//实习
				SdPInternshipExample example = new SdPInternshipExample();
				if(StringUtil.isBlank(status)){
					example.or().andInternshipIdEqualTo(postId)
					.andDelCEqualTo(Constant.DEL_NO);
				}else{
					List<Integer> statusList = new ArrayList<Integer>();
					String[] statusArr = status.split(",");
					for(String statusStr : statusArr){
						statusList.add(Integer.parseInt(statusStr));
					}
					example.or().andInternshipIdEqualTo(postId)
					.andStatusIn(statusList)
					.andDelCEqualTo(Constant.DEL_NO);
				}
				example.setOrderByClause("create_time desc limit " + start + "," + pageSize);
				List<SdPInternship> pInternshipList = personService.selectPInternshipList(example);
				for(SdPInternship pInternship : pInternshipList){
					Integer personId = pInternship.getPersonId();
					Row result = new Row();
					SdPerson person = personService.selectPersonById(personId);
					SdPersonBase personBase = personService.selectPersonBaseById(personId);
					result.put("pPId", pInternship.getpIId());
					result.put("source", pInternship.getSource());
					result.put("personId", personId);
					result.put("postFlag", postFlag);
					result.put("personName", person.getPersonName());
					result.put("personGender", person.getPersonGender() == 0 ? "女" : "男");
					Date birthday = person.getBirthday();
					if(birthday != null){
						result.put("personAge", DateUtil.yearDiffCurr(DateUtil.DateToString(birthday, DateUtil.LONG_DATE_FORMAT)));
					}else{
						result.put("personAge", "");
					}
					result.put("dicFieldName", person.getDicFieldName());
					result.put("university", person.getUniversity());
					result.put("major", person.getMajor());
					result.put("status", pInternship.getStatus());
					Date interviewTime = pInternship.getInterviewTime();
					if(interviewTime != null){
						result.put("interviewTime", DateUtil.DateToString(interviewTime, DateUtil.FORMAT_TWO));
					}else{
						result.put("interviewTime", "");
					}
					String logo = personBase.getLogo();
					if(!StringUtil.isBlank(logo)){
						result.put("logo", Constant.DOMAIN + logo);
					}else{
						result.put("logo", "");
					}
					result.put("dicEducationName", person.getDicEducationName());
					String district = person.getDistrict();
					if(!StringUtil.isBlank(district)){
						int index = district.lastIndexOf(" ");
						if(index != -1){
							district = district.substring(index + 1);
						}
					}else{
						district = "";
					}
					result.put("district", district);
					resultList.add(result);
				}
			}else if(postFlag == Constant.POST_FLAG_PARTTIME){//兼职
				SdPParttimeExample example = new SdPParttimeExample();
				if(status == null){
					example.or().andParttimeIdEqualTo(postId)
						.andDelCEqualTo(Constant.DEL_NO);
				}else{
					List<Integer> statusList = new ArrayList<Integer>();
					String[] statusArr = status.split(",");
					for(String statusStr : statusArr){
						statusList.add(Integer.parseInt(statusStr));
					}
					example.or().andParttimeIdEqualTo(postId)
						.andStatusIn(statusList)
						.andDelCEqualTo(Constant.DEL_NO);
				}
				example.setOrderByClause("create_time desc limit " + start + "," + pageSize);
				List<SdPParttime> pParttimeList = personService.selectPParttimeList(example);
				for(SdPParttime pParttime : pParttimeList){
					Integer personId = pParttime.getPersonId();
					Row result = new Row();
					SdPerson person = personService.selectPersonById(personId);
					SdPersonBase personBase = personService.selectPersonBaseById(personId);
					result.put("pPId", pParttime.getpPId());
					result.put("source", pParttime.getSource());
					result.put("personId", personId);
					result.put("postFlag", postFlag);
					result.put("personName", person.getPersonName());
					result.put("personGender", person.getPersonGender() == 0 ? "女" : "男");
					Date birthday = person.getBirthday();
					if(birthday != null){
						result.put("personAge", DateUtil.yearDiffCurr(DateUtil.DateToString(birthday, DateUtil.LONG_DATE_FORMAT)));
					}else{
						result.put("personAge", 0);
					}
					result.put("dicFieldName", person.getDicFieldName());
					result.put("university", person.getUniversity());
					result.put("major", person.getMajor());
					result.put("status", pParttime.getStatus());
					Date interviewTime = pParttime.getInterviewTime();
					if(interviewTime != null){
						result.put("interviewTime", DateUtil.DateToString(interviewTime, DateUtil.FORMAT_TWO));
					}else{
						result.put("interviewTime", "");
					}
					String logo = personBase.getLogo();
					if(!StringUtil.isBlank(logo)){
						result.put("logo", Constant.DOMAIN + logo);
					}else{
						result.put("logo", "");
					}
					result.put("dicEducationName", person.getDicEducationName());
					result.put("district", person.getDistrict());
					resultList.add(result);
				}
			}
			return new ResponseMsg("操作成功", ErrorCode.OK, resultList);
			}catch(Exception e){
				e.printStackTrace();
				return new ResponseMsg("操作失败", ErrorCode.SERVER_ERROR, null);
			}
		}
	
	
	
	@RequestMapping(value = { "/updateMyResume.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg updateMyResume(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer companyId = RequestUtil.getInt(request, "uid");
			Integer pPId = RequestUtil.getInt(request, "pPId");
			Integer postFlag = RequestUtil.getInt(request, "postFlag");//1实习2兼职
			Integer status = RequestUtil.getInt(request, "status", 1);//状态 默认1已查看
			String interviewTime = RequestUtil.get(request, "interviewTime");//面试时间
			Integer delC = RequestUtil.getInt(request, "del");//企业删除
			if(companyId == null || pPId == null || postFlag == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			int operCount = 0;
			if(postFlag == Constant.POST_FLAG_INTERNSHIP){//实习
				SdPInternship pInternship = new SdPInternship();
				pInternship.setpIId(pPId);
				pInternship.setStatus(status);
				if(!StringUtil.isBlank(interviewTime)){
					pInternship.setInterviewTime(DateUtil.StringtoDate(interviewTime, DateUtil.FORMAT_TWO));
				}
				pInternship.setDelC(delC);
				operCount = personService.updatePInternship(pInternship);
			}else if(postFlag == Constant.POST_FLAG_PARTTIME){//兼职
				SdPParttime pParttime = new SdPParttime();
				pParttime.setpPId(pPId);
				pParttime.setStatus(status);
				if(!StringUtil.isBlank(interviewTime)){
					pParttime.setInterviewTime(DateUtil.StringtoDate(interviewTime, DateUtil.FORMAT_TWO));
				}
				pParttime.setDelC(delC);
				operCount = personService.updatePParttime(pParttime);
			}
			if(operCount > 0){
				return new ResponseMsg("操作成功", ErrorCode.OK, null);
			}else{
				return new ResponseMsg("简历记录不存在", ErrorCode.ERROR, null);
			}
			}catch(Exception e){
				e.printStackTrace();
				return new ResponseMsg("操作失败", ErrorCode.SERVER_ERROR, null);
			}
		}
	
	@RequestMapping(value = { "/invite.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg invite(HttpServletRequest request, HttpServletResponse response) {
		try {
			Integer personId = RequestUtil.getInt(request, "personId");
			Integer postId = RequestUtil.getInt(request, "postId");
			Integer postFlag = RequestUtil.getInt(request, "postFlag");
			if(personId == null || postId == null || postFlag == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			if(postFlag == Constant.POST_FLAG_INTERNSHIP){//邀请实习即通知面试
				SdPInternship pInternship = new SdPInternship();
				pInternship.setPersonId(personId);
				pInternship.setInternshipId(postId);
				pInternship.setCreateTime(new Date());
				pInternship.setStatus(Constant.INTERNSHIP_STATUS_APPLY);
				pInternship.setSource(Constant.SOURCE_COMPANY);
				pInternship.setDelC(Constant.DEL_NO);
				pInternship.setDelP(Constant.DEL_NO);
				personService.insertPInternship(pInternship);
			}else if(postFlag == Constant.POST_FLAG_PARTTIME){
				SdPParttime pParttime = new SdPParttime();
				pParttime.setPersonId(personId);
				pParttime.setParttimeId(postId);
				pParttime.setCreateTime(new Date());
				pParttime.setStatus(Constant.INTERNSHIP_STATUS_APPLY);
				pParttime.setSource(Constant.SOURCE_COMPANY);
				pParttime.setDelC(Constant.DEL_NO);
				pParttime.setDelP(Constant.DEL_NO);
				personService.insertPParttime(pParttime);
			}
			return new ResponseMsg("操作成功", ErrorCode.OK, null);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseMsg("操作失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/getInvitationFlag.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg getInvitationFlag(HttpServletRequest request, HttpServletResponse response) {
		try {
			Integer personId = RequestUtil.getInt(request, "personId");
			Integer postId = RequestUtil.getInt(request, "postId");
			Integer postFlag = RequestUtil.getInt(request, "postFlag");
			if(personId == null || postId == null || postFlag == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			Row result = new Row();
			if(postFlag == Constant.POST_FLAG_INTERNSHIP){//邀请实习即通知面试
				SdPInternshipExample example = new SdPInternshipExample();
				example.or().andPersonIdEqualTo(personId)
					.andInternshipIdEqualTo(postId)
					.andSourceEqualTo(Constant.SOURCE_COMPANY)
					.andDelCEqualTo(Constant.DEL_NO);
				List<SdPInternship> list = personService.selectPInternshipList(example);
				if(list != null && list.size() > 0){
					result.put("invitationFlag", 1);
				}else{
					result.put("invitationFlag", 0);
				}
			}else if(postFlag == Constant.POST_FLAG_PARTTIME){
				SdPParttimeExample example = new SdPParttimeExample();
				example.or().andPersonIdEqualTo(personId)
					.andParttimeIdEqualTo(postId)
					.andSourceEqualTo(Constant.SOURCE_COMPANY)
					.andDelCEqualTo(Constant.DEL_NO);
				List<SdPParttime> list = personService.selectPParttimeList(example);
				if(list != null && list.size() > 0){
					result.put("invitationFlag", 1);
				}else{
					result.put("invitationFlag", 0);
				}
			}
			return new ResponseMsg("查询成功", ErrorCode.OK, result);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseMsg("查询失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	
	
	
		public static void main(String[] args) {
		
	}
	
}
